﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Reflection;
using System.ComponentModel;
using EIIS.FrameWork.Cache;
using EIIS.Infrastructure.Model.System;
using EIIS.Infrastructure.Model.System.Authority;
using Spring.Context;
using Spring.Context.Support;


namespace EIIS.Presentation.Controllers.CommonSupport
{
    public class CurrentUser
    {
        public CurrentUser()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                IApplicationContext ctx = ContextRegistry.GetContext();
                ICache cache = (ICache)ctx.GetObject("Cache");
                if (cache.GetSessionCache("HospUser") != null)
                {
                    HospUser user = (HospUser)cache.GetSessionCache("HospUser");
                    UserInfo = user;

                    List<Role> list = user.Roles.ToList<Role>();

                    List<MenuNode> tempMenuPermission = new List<MenuNode>();
                    foreach (Role tempRole in list)
                    {
                        tempMenuPermission.AddRange(tempRole.MenuNodes.ToList<MenuNode>());
                    }
                    MenuPermission = tempMenuPermission;

                    List<ActionPermission> tempActionPermission = new List<ActionPermission>();
                    foreach (Role tempRole in list)
                    {
                        tempActionPermission.AddRange(tempRole.ActionPermissions.ToList<ActionPermission>());
                    }
                    ActionPermission = tempActionPermission;

                    cache.SetSessionCache("CurrentUser", this);
                }
            }                      
        }

        //用户信息
        public Person UserInfo { get; set; }

        //菜单权限
        public IList<MenuNode> MenuPermission { get; set; }

        //功能权限
        public IList<ActionPermission> ActionPermission { get; set; }


        #region 超级管理员权限

        //private void SuperMenu(ICache cache)
        //{
        //    var result = new List<MenuNode>();

        //    //根节点
        //    MenuNode superRoot = new MenuNode()
        //    {
        //        Leaf = false,
        //        Level = 1,
        //        ActionName = "",
        //        ControllerName = "",
        //        Description = "超级管理员权限",
        //        Index = 1,
        //        Name = "超级权限",
        //        ParentId = "0",
        //        TreeCode = "001",
        //        Url = ""
        //    };

        //    if (cache.GetApplicationCache("allmenus") == null)
        //    {
        //        result.Add(superRoot);
        //        var types = Assembly.Load("EIIS.Controllers").GetTypes();
        //        foreach (var type in types)
        //        {
        //            if (type.BaseType.Name == "BaseController")
        //            {
        //                var methods = type.GetMethods();
        //                foreach (var method in methods)
        //                {
        //                    var attributes = method.GetCustomAttributes(true);
        //                    int i = 0;

        //                    foreach (var attribute in attributes)
        //                    {
        //                        if (attribute.ToString() == "EIIS.Controllers.CommonSupport.ViewPageAttribute")
        //                        {
        //                            ViewPageAttribute viewpage = attribute as ViewPageAttribute;

        //                            if (method.ReturnType.Name == "ActionResult" && viewpage.IsMenu)
        //                            {
        //                                i++;
        //                                var menu = new MenuNode()
        //                                {
        //                                    IsExtPage = true,
        //                                    Leaf = true,
        //                                    BigIcon = "",
        //                                    Level = 2,
        //                                    Index = 1,
        //                                    ParentId = superRoot.Id,
        //                                    SmallIcon = "",
        //                                    TreeCode = "001" + i.ToString(),
        //                                };
        //                                menu.ActionName = method.Name;
        //                                menu.ControllerName = method.DeclaringType.Name.Substring(0, method.DeclaringType.Name.Length - 10); // 去掉“Controller”后缀
        //                                object[] attrs = method.GetCustomAttributes(typeof(DescriptionAttribute), true);
        //                                if (attrs.Length > 0)
        //                                    menu.Description = (attrs[0] as DescriptionAttribute).Description;

        //                                menu.Name = menu.Description != null ? menu.Description : menu.ActionName;
        //                                menu.URL = MenuNode.GetURL(menu.ControllerName, menu.ActionName);

        //                                result.Add(menu);
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        cache.SetApplicationCache("allmenus", result);
        //    }
        //    else
        //    {
        //        result = cache.GetApplicationCache("allmenus") as List<MenuNode>;
        //    }

        //    MenuPermission = result.ToList<MenuNode>();
        //}

        #endregion
    }
}
